package com.ce.pms.charge.dao.impl;

import java.util.List;
import org.hibernate.Query;
import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.IPeriodLogDao;
import com.ce.pms.charge.model.PeriodLogDomain;
import com.ce.pms.charge.vo.MonthlyStatementSearchVO;
import com.ce.pms.common.annotation.AssociatedPrecinctQuery;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

public class PeriodLogDaoImpl extends PmsBaseDaoImpl<PeriodLogDomain> implements IPeriodLogDao {

	/**
	 * 功能描述:根据收费周期及流程ID查询日志信息  <br>
	 * @param periodId 收费周期ID
	 * @param flowDefId  流程ID
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public List<PeriodLogDomain> findLogByPeriodAndFlowDef(Long periodID, Integer flowDefID) {
		String queryStr = "FROM PeriodLogDomain pl where pl.period.keyword ="+periodID+" and  pl.flowDef.seqNo = "+flowDefID;
		Query query = createQuery(queryStr,null);
		return query.list();
	}

	/**
	 * 功能描述:检查是否有已经做过应收计算的流程,只要有一个都返回ture。<br>
	 * 如果buildId不为null，则检查该楼栋所在的收费周期是否做过应收计算<br>
	 * @param buildingId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-26<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public boolean checkPeriodHasArcaculate(Long buildingId){
		StringBuilder queryStr =new StringBuilder( "select pl FROM PeriodLogDomain pl JOIN pl.period.chargePrecinct.buildings bl WHERE  pl.flowDef.seqNo = ? AND pl.period.validity=? ");
		if(null!=buildingId){
			queryStr.append(" AND bl.keyword =").append(buildingId);
		}
		Query query = createQuery(queryStr.toString(), SysrefcodeDO.FEE_FLOW_DEF_NAME_RECEIVABLE.shortValue(),
								SysrefcodeDO.FEE_PERIOD_VALIDITY_VALID);
		return query.list().size()>0;
	}


	/**
	 * 功能描述:检查是否有已经做过收费通知单的流程,只要有一个都返回ture<br>
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-9-26<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public boolean checkPeriodHasNote(){
		String queryStr = "FROM PeriodLogDomain pl WHERE  pl.flowDef.seqNo = ? AND pl.period.validity=?";
		Query query = createQuery(queryStr, SysrefcodeDO.FEE_FLOW_DEF_NAME_NOTICE,
								SysrefcodeDO.FEE_PERIOD_VALIDITY_VALID);
		return query.list().size()>0;
	}

	/**
	 * 功能描述: 根据条件分页查询已月结信息<br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public Page queryCurPageMonthlyStatement(MonthlyStatementSearchVO searchVO) {

		String queryStr = this.getCurPageHQL(searchVO);

		List list = this.getCurrentPageList(searchVO,queryStr);
		int startIndex  = super.getFirstRow(searchVO.getPageNo(), searchVO.getPageSize());//获取开始行
		int count = this.getMonthlyStatementCount(searchVO,queryStr);
		int pageSize = searchVO.getPageSize();
		Page page = new Page(startIndex,count,pageSize,list);

		return page;
	}

	/**
	 * 功能描述:拼写分页查询月结列表HQL语句  <br>
	 * @param searchVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private String getCurPageHQL(MonthlyStatementSearchVO searchVO){
		StringBuffer queryStr = new StringBuffer();
//		queryStr.append(" SELECT new list(pl.keyword,pl.period.keyword,pl.period.period,pl.endTime,sui.username ) ");
//		queryStr.append(" FROM PeriodLogDomain  pl ,SecUserinfoDO sui  ");
//		queryStr.append(" WHERE pl.period.validity = "+SysrefcodeDO.FEE_PERIOD_VALIDITY_INVALIDATION);
//		queryStr.append(" AND pl.flowDef.seqNo = " + SysrefcodeDO.FEE_FLOW_DEF_NAME_MONTHLYSTATEMENT);
//		queryStr.append(" AND pl.creator = sui.userId ");
//		if (!BlankUtil.isBlank(searchVO.getPeriod())){
//			queryStr.append(" and pl.period.period = " + searchVO.getPeriod());
//		}
//		queryStr.append(" group by pl.period.period ");
//		queryStr.append(" order by pl.endTime  " + searchVO.getSortStr());

		queryStr.append(" SELECT new list(pl.keyword,pl.period.keyword,pl.period.period,pl.endTime,pl.creator ) ");
		queryStr.append(" FROM PeriodLogDomain  pl  ");
		queryStr.append(" WHERE 1=1 AND pl.period.validity = "+SysrefcodeDO.FEE_PERIOD_VALIDITY_INVALIDATION);
		queryStr.append(" AND pl.flowDef.seqNo = " + SysrefcodeDO.FEE_FLOW_DEF_NAME_MONTHLYSTATEMENT);
		if (!BlankUtil.isBlank(searchVO.getPeriod())){
			queryStr.append(" and pl.period.period = " + searchVO.getPeriod());
		}
		queryStr.append(" group by pl.period.period ");
		queryStr.append(" order by pl.endTime  " + searchVO.getSortStr());

		return queryStr.toString();
	}

	/**
	 * 功能描述:根据条件查询已月结月份的记录总数   <br>
	 * @param searchVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@AssociatedPrecinctQuery(alias="pl", beginStr=" AND ", endStr=" ", insertAfterPoint="WHERE 1=1",property4Precinct="period.chargePrecinct.keyword")
	private int getMonthlyStatementCount(MonthlyStatementSearchVO searchVO,String hql) {
		Query query = createQuery(hql.toString(), null);
		List list =	query.list();
		if (BlankUtil.isBlank(list))
		{
			return 0;
		}
		int count =list.size();
		return count;
	}

	/**
	 * 功能描述:分页查询已月结信息  <br>
	 * @param searchVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@AssociatedPrecinctQuery(alias="pl", beginStr=" AND ", endStr=" ", insertAfterPoint="WHERE 1=1",property4Precinct="period.chargePrecinct.keyword")
	private List getCurrentPageList(MonthlyStatementSearchVO searchVO,String hql){

		Query query = createQuery(hql, null);
		int firstResult = (searchVO.getPageNo() - 1)* searchVO.getPageSize();
		query.setFirstResult(firstResult);
		query.setMaxResults(searchVO.getPageSize());

		return query.list();
	}
}
